<!DOCTYPE html>
<html lang="en">

<head>
    <title>品学联萌</title>
    <#include "/pxlm/app/product/components/meta.html" />
    <#include "/pxlm/app/product/components/stylesheets.html" />
    <link rel="stylesheet" href="/pxlm/app/product/stylesheets/cart.css?v=1">
</head>

<body>
    <div id="app">
        <header data-am-widget="header" class="am-header am-header-default am-header-fixed" style="background:url('http://app.cenjiawen.com/pxlm/img/pxlmappbanner.png') no-repeat center">
            <h1 class="am-header-title">购物车</h1>
        </header>
        <div class="majors">
            <div class="majors-list" v-if="cart && cart.length > 0">
                <div class="majors-item" v-for="(item, index) in cart" :key="index">
                    <div class="major-checkbox am-checkbox">
                        <label>
                            <input type="checkbox" :ckecked="indexToSubmit[index]" @change="selectItem(item)">
                        </label>
                    </div>
                    <div class="major-content">
                        <div class="major-title" @click="goMajorDetail(item)">{{ item.title }}</div>
                        <div class="major-subtitle">{{ item.formatTitle }}</div>
                    </div>
                </div>
            </div>
            <div class="no-item" v-else>
                <span>购物车为空</span>
            </div>
        </div>
        <div class="pay" @click="pay">结算</div>
        <#include "/pxlm/app/product/components/tabbar.html" />
    </div>
    <#include "/pxlm/app/product/components/scripts.html" />
    <script type="text/javascript">
    var app = new Vue({
        el: '#app',
        data() {
            return {
                cart: [],
                indexToSubmit: []
            }
        },
        mounted: function() {
            this.init();
        },
        methods: {
            init() {
                var url = '${host!}/pxlm/app/product/api/cart';
                var params = {};
                var success = res => {
                    console.log(res);
                    this.cart = res.info;
                    this.indexToSubmit = new Array(this.cart.length);
                }
                var fail = res => {
                    console.log(res);
                }
                var complete = () => {}
                request(url, params, success, fail, complete);
            },
            selectItem(item) {
                if (!this.indexToSubmit[this.cart.indexOf(item)]) {
                    this.indexToSubmit[this.cart.indexOf(item)] = true;
                } else {
                    this.indexToSubmit[this.cart.indexOf(item)] = false;
                }
            },
            goMajorDetail(item) {
                window.location.href = '/pxlm/app/product/majorInfo?productId=' + item.id;
            },
            pay() {
                var empty = true;
                for (var i = 0; i < this.indexToSubmit.length; i ++) {
                    if (this.indexToSubmit[i]) {
                        empty = false;
                    }
                }
                if (empty) {
                    msg("请勾选至少一个课程");
                    return;
                }
                var cart = new Array;
                for (var i = 0; i < this.indexToSubmit.length; i ++) {
                    if (this.indexToSubmit[i]) {
                        cart.push(this.cart[i]);
                    }
                }
                sessionStorage.setItem("cart", JSON.stringify(cart));
                window.location.href = '/pxlm/app/product/enroll';
            }
        }
    })
    </script>
</body>

</html>